package cn.iocoder.yudao.module.erp.service.purchaseplan.purchaseminordetail;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.erp.controller.admin.purchaseplan.vo.purchaseminordetail.PurchaseMinorDetailPageReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.purchaseplan.vo.purchaseminordetail.PurchaseMinorDetailSaveReqVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.purchaseplan.purchaseminordetail.PurchaseMinorDetailDO;
import cn.iocoder.yudao.module.erp.dal.mysql.purchaseplan.purchaseminordetail.PurchaseMinorDetailMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.erp.enums.ErrorCodeConstants.PURCHASE_MINOR_DETAIL_NOT_EXISTS;

/**
 * 小额采购计划明细 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class PurchaseMinorDetailServiceImpl implements PurchaseMinorDetailService {

    @Resource
    private PurchaseMinorDetailMapper purchaseMinorDetailMapper;

    @Override
    public void createPurchaseMinorDetailBatch(List<PurchaseMinorDetailSaveReqVO> createReqVOS) {
        // 插入
        List<PurchaseMinorDetailDO> purchaseMinorDetailS = BeanUtils.toBean(createReqVOS, PurchaseMinorDetailDO.class);
        purchaseMinorDetailMapper.insert(purchaseMinorDetailS);
    }

    @Override
    public void updatePurchaseMinorDetail(PurchaseMinorDetailSaveReqVO updateReqVO) {
        // 校验存在
        validatePurchaseMinorDetailExists(updateReqVO.getId());
        // 更新
        PurchaseMinorDetailDO updateObj = BeanUtils.toBean(updateReqVO, PurchaseMinorDetailDO.class);
        purchaseMinorDetailMapper.updateById(updateObj);
    }

    @Override
    public void deletePurchaseMinorDetail(Long id) {
        // 校验存在
        validatePurchaseMinorDetailExists(id);
        // 删除
        purchaseMinorDetailMapper.deleteById(id);
    }

    @Override
        public void deletePurchaseMinorDetailListByIds(List<Long> ids) {
        // 校验存在
        validatePurchaseMinorDetailExists(ids);
        // 删除
        purchaseMinorDetailMapper.deleteByIds(ids);
        }

    private void validatePurchaseMinorDetailExists(List<Long> ids) {
        List<PurchaseMinorDetailDO> list = purchaseMinorDetailMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(PURCHASE_MINOR_DETAIL_NOT_EXISTS);
        }
    }

    private void validatePurchaseMinorDetailExists(Long id) {
        if (purchaseMinorDetailMapper.selectById(id) == null) {
            throw exception(PURCHASE_MINOR_DETAIL_NOT_EXISTS);
        }
    }

    @Override
    public PurchaseMinorDetailDO getPurchaseMinorDetail(Long id) {
        return purchaseMinorDetailMapper.selectById(id);
    }

    @Override
    public PageResult<PurchaseMinorDetailDO> getPurchaseMinorDetailPage(PurchaseMinorDetailPageReqVO pageReqVO) {
        return purchaseMinorDetailMapper.selectPage(pageReqVO);
    }

    @Override
    public void deleteByPlanId(Long planId) {
        purchaseMinorDetailMapper.deleteByPlanId(planId);
    }

    @Override
    public List<PurchaseMinorDetailDO> getByPlanId(Long planId) {
        return purchaseMinorDetailMapper.selectByPlanId(planId);
    }

}